package com.lijianlove.keeplearning;

import com.lijianlove.keeplearning.dr.test.DRService;
import com.lijianlove.keeplearning.entity.User;
import com.lijianlove.keeplearning.multDatasource.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@EnableAsync
@SpringBootApplication
@RestController
@ComponentScan("com.lijianlove.keeplearning.**,com.github.qinxuewu.core")
public class KeepLearningApplication {

    public static void main(String[] args) {
        SpringApplication.run(KeepLearningApplication.class, args);
    }


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private UserService userService;
    @Autowired
    private DRService drService;

    @RequestMapping("/{id}")
    public String hello(@PathVariable Integer id) {
        String sql = "select * from appuser";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return "hello " + id;
    }

    @RequestMapping("/selectAll/{id}")
    public String selectAll(@PathVariable Integer id) {
        List<Map<String, Object>> maps = userService.selectAll();
        return "hello " + id;
    }

    @RequestMapping("/selectByCondition/{id}")
    public String selectByCondition(@PathVariable Integer id) {
        List<Map<String, Object>> maps = userService.selectByCondition();
        return "hello " + id;
    }

    @RequestMapping("/save")
    public String save() {
        userService.save();
        return "saved";
    }

    @RequestMapping("/testTransactional")
    public String testTransactional() {
        User user = new User();
        user.setId(2L);
        userService.testTransactional(user);
        return "saved";
    }

    @RequestMapping("/testdr")
    public String testdr() {
        User user = new User();
        user.setId(2L);
        user.setAge(10);
        user.setName("zhangsan");
        drService.testdr(user);
        return "saved";
    }


}
